const express = require('express');
const router = express.Router();
const { authenticateToken, checkRole } = require('../common/auth');
const productController = require('../controller/productController');

/**
 * @swagger
 * /api/products/categories:
 *   post:
 *     tags: [Products]
 *     summary: 创建商品分类
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *             properties:
 *               name:
 *                 type: string
 *               description:
 *                 type: string
 *     responses:
 *       201:
 *         description: 分类创建成功
 */
router.post('/categories', authenticateToken, checkRole(['admin']), productController.createCategory);

/**
 * @swagger
 * /api/products/categories:
 *   get:
 *     tags: [Products]
 *     summary: 获取所有商品分类
 *     responses:
 *       200:
 *         description: 成功获取分类列表
 */
router.get('/categories', productController.getAllCategories);

/**
 * @swagger
 * /api/products:
 *   post:
 *     tags: [Products]
 *     summary: 创建商品
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *               - category_id
 *               - price
 *             properties:
 *               name:
 *                 type: string
 *               category_id:
 *                 type: integer
 *               price:
 *                 type: number
 *               stock:
 *                 type: integer
 *               description:
 *                 type: string
 *               image_url:
 *                 type: string
 *     responses:
 *       201:
 *         description: 商品创建成功
 */
router.post('/', authenticateToken, checkRole(['admin']), productController.createProduct);

/**
 * @swagger
 * /api/products:
 *   get:
 *     tags: [Products]
 *     summary: 获取商品列表
 *     parameters:
 *       - in: query
 *         name: category_id
 *         schema:
 *           type: integer
 *         description: 按分类筛选
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *         description: 按状态筛选
 *       - in: query
 *         name: min_price
 *         schema:
 *           type: number
 *         description: 最低价格
 *       - in: query
 *         name: max_price
 *         schema:
 *           type: number
 *         description: 最高价格
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 10
 *         description: 每页数量
 *     responses:
 *       200:
 *         description: 成功获取商品列表
 */
router.get('/', productController.getAllProducts);

/**
 * @swagger
 * /api/products/{id}:
 *   get:
 *     tags: [Products]
 *     summary: 获取商品详情
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 商品ID
 *     responses:
 *       200:
 *         description: 成功获取商品详情
 */
router.get('/:id', productController.getProductById);

/**
 * @swagger
 * /api/products/{id}:
 *   put:
 *     tags: [Products]
 *     summary: 更新商品信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 商品ID
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *               category_id:
 *                 type: integer
 *               price:
 *                 type: number
 *               stock:
 *                 type: integer
 *               description:
 *                 type: string
 *               image_url:
 *                 type: string
 *               status:
 *                 type: string
 *     responses:
 *       200:
 *         description: 商品信息更新成功
 */
router.put('/:id', authenticateToken, checkRole(['admin']), productController.updateProduct);

/**
 * @swagger
 * /api/products/{id}/stock:
 *   patch:
 *     tags: [Products]
 *     summary: 更新商品库存
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 商品ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - stock
 *             properties:
 *               stock:
 *                 type: integer
 *                 description: 新的库存数量
 *     responses:
 *       200:
 *         description: 库存更新成功
 */
router.patch('/:id/stock', authenticateToken, checkRole(['admin']), productController.updateStock);

/**
 * @swagger
 * /api/products/{id}:
 *   delete:
 *     tags: [Products]
 *     summary: 删除商品
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 商品ID
 *     responses:
 *       200:
 *         description: 商品删除成功
 */
router.delete('/:id', authenticateToken, checkRole(['admin']), productController.deleteProduct);

module.exports = router; 